const fs = require('fs')
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')

const appDirectory = fs.realpathSync(process.cwd())
const resolveApp = relativePath => path.resolve(appDirectory, relativePath)
const app = [
  {
    key: 'articleHome/index',
    indexJs: './src/articleHome/index.js',
    indexPage: './src/articleHome/index.html',
  },
  {
    key: 'homePage/index',
    indexJs: './src/homePage/index.js',
    indexPage: './src/homePage/index.html',
  },
  {
    key: 'userHome/index',
    indexJs: './src/userHome/index.js',
    indexPage: './src/userHome/index.html',
  },
]

function getEntry (app) {
  let entry = {}
  app.forEach(item => {
    entry[item.key] = [
      // 热加载
      require.resolve('react-dev-utils/webpackHotDevClient'),
      // 使用当前文件所在目录为基础，拼接item的indexjs
      resolveApp(item.indexJs)
    ]
  })
  return entry
}

function getPlugin (app) {
  let plugin = []
  app.forEach(item => {
    plugin.push(new HtmlWebpackPlugin({
      template: item.indexPage,
      filename: item.key + '.html',
      chunks: [item.key]
    }))
  })
  return plugin
}

module.exports = {
  blogEntry: getEntry(app),
  blogPlugin: getPlugin(app),
}
